package sctu.edu.java.year2016.team01.xuhanmei.day20160607.chapter15_2;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Administrator on 2016/6/12.
 * 尝试开发一个窗体，在窗体中有两个按钮，一个是“开始”按钮，一个是“结束”按钮，
 * 当单击“开始”按钮时，在控制台中持续打印一段话，当用户单击“停止”按钮时，控制台结束打印
 */
public class PrintFrame extends JFrame {
    private JPanel contentPane;
    Timer timer;
    int index=1;
    private JTextArea ita;

    public static void main(String[] args) {
        PrintFrame frame=new PrintFrame();
        frame.setVisible(true);
    }
    public PrintFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 200);
        setLocationRelativeTo(null);
        contentPane=new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel=new JPanel();
        contentPane.add(panel,BorderLayout.SOUTH);

        JButton jstart=new JButton("开始");
        panel.add(jstart);
        timer=new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ita.append("打印第"+index+"句话\n");
                index++;
            }
        });
        jstart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.start();
            }
        });
        JButton jend=new JButton("停止");
        panel.add(jend);
        jend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
            }
        });
        JScrollPane scrollPane=new JScrollPane();
        contentPane.add(scrollPane,BorderLayout.CENTER);
        ita=new JTextArea();
        scrollPane.setViewportView(ita);
    }
}
